package org.han.org.controller;

import io.swagger.annotations.ApiOperation;
import org.han.annotation.RonghuanetPermission;
import org.han.org.domain.Department;
import org.han.org.service.impl.DepartmentServiceImpl;
import org.han.org.query.DepartmentQuery;
import org.han.util.AjaxResult;
import org.han.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@RonghuanetPermission(name="部门管理",desc = "部门的增删改查")
public class DepartmentController {
    @Autowired
    private DepartmentServiceImpl departmentService;

    // 新增 / 修改
    @PutMapping
    @ApiOperation(value = "新增/修改" )
    @RonghuanetPermission(name="部门的新增/修改")
    public AjaxResult addorudpate(@RequestBody Department department){
        try {
            if(Objects.isNull(department.getId())){
                // 新增
                departmentService.insert(department);
                return AjaxResult.me();
            }else {
                // 修改
                departmentService.update(department);
                return AjaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败~" + e.getMessage());
        }
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除" )
    @RonghuanetPermission(name="部门的删除")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败~"+ e.getMessage());
        }
    }

    // 查询一个
    @GetMapping("{id}")
    @ApiOperation(value = "通过ID查询" )
    @RonghuanetPermission(name="通过ID查询部门")
    public AjaxResult seleteById(@PathVariable("id")Long id ){
        try {
            Department department = departmentService.selectById(id);
            return AjaxResult.me().setData(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败~"+ e.getMessage());
        }
    }

    // 查询全部
    @GetMapping
    @ApiOperation(value = "查询所有" )
    @RonghuanetPermission(name="查询所有部门")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return AjaxResult.me().setData(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!" + e.getMessage());
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    @RonghuanetPermission(name="部门的分页查询")
    public AjaxResult pageList(@RequestBody DepartmentQuery query){
        try {
            PageList queryList = departmentService.pageList(query);
            return AjaxResult.me().setData(queryList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!" + e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation("批量删除")
    @RonghuanetPermission(name="部门的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!" + e.getMessage());
        }
    }

    @GetMapping("/tree")
    @ApiOperation("查询部门树")
    @RonghuanetPermission(name="通查询部门树")
    public AjaxResult getDepTree(){
        try {
            List<Department> tree = departmentService.tree();
            return AjaxResult.me().setData(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败~" + e.getMessage());
        }
    }
}
